<template>
  <a-modal
    title="目标班级"
    :width="500"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item style="display: none;" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input v-decorator="['formClassId']" />
        </a-form-item>
        <a-form-item label="目标班级" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-select allowClear v-decorator="['toClassId', { rules: [{ required: true, message: '请选择目标班级' }] }]">
            <a-select-option v-for="item in classList" :key="item.id" :value="item.id">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
import { sysPosGoUp, sysPosList } from '@/api/modular/garden/classManage'
export default {
  data() {
    return {
      visible: false,
      confirmLoading: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 }
      },
      form: this.$form.createForm(this),
      classList: []
    }
  },
  methods: {
    edit(record) {
      this.visible = true
      this.getClassList()
      this.$nextTick(() => {
        this.form.setFieldsValue({
          formClassId: record.id
        })
      })
    },
    getClassList() {
      sysPosList().then(res => {
        this.classList = res.data.filter(item => item.id !== this.form.getFieldValue('formClassId'))
      })
    },
    handleSubmit() {
      const {
        form: { validateFields }
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          sysPosGoUp(values)
            .then(res => {
              if (res.success) {
                this.$message.success('升班成功')
                this.handleCancel()
              } else {
                this.$message.error('升班失败：' + res.message)
              }
            })
            .finally(() => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
      this.$emit('ok')
    }
  }
}
</script>
